﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Streambolics.Hp41s
{
    /// <summary>
    ///     A factory corresponding to a fully constructed instruction.
    /// </summary>

    public class ReadyMadeInstructionFactory : InstructionFactory
    {
        private Instruction _Instruction;

        public ReadyMadeInstructionFactory (string aName, Instruction aInstruction)
            : base (aName)
        {
            _Instruction = aInstruction;
        }

        public ReadyMadeInstructionFactory (string aName)
            : this (aName, null)
        {
        }

        protected virtual Instruction ComputeInstruction ()
        {
            return null;
        }

        /// <summary>
        ///     The instruction.
        /// </summary>
        /// <returns>
        ///     A complete instruction.
        /// </returns>
        /// <remarks><para>
        ///     The default implementation of ToInstruction is
        ///     to call ComputeInstruction() to compute the
        ///     actual instruction, and cache the result. It is 
        ///     almost always a good idea to redefine ComputeInstruction
        ///     instead of ToInstruction().
        /// </para></remarks>

        public override Instruction ToInstruction ()
        {
            if (_Instruction == null)
            {
                _Instruction = ComputeInstruction ();
            }
            return _Instruction;
        }

        public override Instruction Parse (Tokenizer aTokenizer)
        {
            return ToInstruction();
        }

        public override bool InstructionAvailable
        {
            get
            {
                return true;
            }
        }

        public override InstructionFactory InterpretKey (int aKey)
        {
            throw new EngineException ("ReadyMadeInstruction should not interpret keys");
        }
        
    }
}
